import { defineStore } from 'pinia'
import { Name } from './store-name'

// 定义一个异步的方法
type User = {
  name: string,
  age: number | null
}
const getUser = (): Promise<User> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        name: '阿夸',
        age: 21
      })
    }, 1000);
  })
}

// 创建一个仓库
export const useTest = defineStore(Name.TEST, {
  // state要写成一个函数的形式
  state: () => {
    return {
      name: '阿夸',
      count: 0
    }
  },
  // 计算属性
  getters: {
    newName(): string {
      return this.name + this.count
    }
  },
  // 同步异步都支持
  actions: {
    setName(value: string) {
      this.name = '阿-夸'
      console.log(value);
    },

    // 调用异步方法
    async user() {
      let data = await getUser()
      console.log(data);
    }

  }
}) 